/**
 * 
 */
/**
 * ISSG Sony China 2011 
 *
 */
package com.dragonsoftbravo.base.filter;

import java.io.IOException;
import java.util.Set;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import cn.com.dragonsoftbravo.utils.BaseWebConstants;
import cn.com.dragonsoftbravo.utils.web.RequestUtils;

import com.google.common.collect.Sets;

/**
 * 
 * @copyright Promoters Sell-thru System, ISSG, Sony China
 * 
 * @author Den
 * @date 2011-8-26
 * @version 0.1
 */
public class HttpSessionFilter implements Filter {

	private final static String TIMEOUT_REDIRECT = "timeoutRedirect";

	private String redirectRUL = "/login.jsp";
	
	private final static Set<String> EXCEPT_URI = Sets.newConcurrentHashSet();

	static{
		EXCEPT_URI.add("/index.html");
		EXCEPT_URI.add("/mvc/mobile/login");
		EXCEPT_URI.add("/mvc/maintenance/basicInformation/maintain");
		EXCEPT_URI.add("/mvc/account/login");
		EXCEPT_URI.add("/cross_domain_upload.js");
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

		String timeoutRedirect = filterConfig.getInitParameter(TIMEOUT_REDIRECT);

		if (StringUtils.isNotBlank(timeoutRedirect)) {
			setRedirectRUL(timeoutRedirect);
		}
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {

		if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) {
			throw new ServletException("HttpSessionFilter just supports HTTP requests");
		}

		HttpServletRequest httpRequest = (HttpServletRequest) request;
		HttpServletResponse httpResponse = (HttpServletResponse) response;
		if (shouldContinueFilter(httpRequest)) {
			chain.doFilter(request, response);
		} else {

			if (httpRequest.getHeader("x-requested-with") != null
					&& httpRequest.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) {

				httpResponse.setStatus(911);// 表示session timeout
			} else {
				/*
				 * handle for mobile request William Shen 2013-12-6
				 */
				if (RequestUtils.isMobileAPI(httpRequest)) {
					// httpResponse.setStatus(911);// session timeout for Mobile
					httpResponse.sendError(911); // API

				} else {
					httpResponse.sendRedirect(httpRequest.getContextPath() + getRedirectRUL());
				}

			}

		}
	}

	protected boolean shouldContinueFilter(HttpServletRequest httpRequest) {
		return isSessionValid(httpRequest) || notRequiredSessionUri(httpRequest);
	}

	protected boolean notRequiredSessionUri(HttpServletRequest httpRequest) {
		
		String uri = httpRequest.getRequestURI();
		boolean notRequired = false;
		for(String str : EXCEPT_URI){
			 if(StringUtils.contains(uri, str)){
				 notRequired = true;
				 break;
			 }
		}
		return  notRequired;

	}

	protected boolean isSessionValid(HttpServletRequest httpRequest) {
		return (httpRequest.isRequestedSessionIdValid() && httpRequest.getSession().getAttribute(
				BaseWebConstants.LOGIN_USER_ID) != null);
	}

	public String getRedirectRUL() {
		return redirectRUL;
	}

	public void setRedirectRUL(String redirectRUL) {
		this.redirectRUL = redirectRUL;
	}

	@Override
	public void destroy() {

	}
}
